import { observable, makeObservable, action, flow, computed, autorun } from 'mobx'

class Apples {
  @observable apples = []
  @observable eatedApplesCount = 0
  @observable appleCount = 0
  @observable eatedApples = []

  constructor () {
    makeObservable(this)
  }

  @action.bound pickApple () {
    this.apples.push({
      weight: Math.floor(Math.random() * 100 + 200),
      id: this.appleCount
    })
    this.appleCount = this.appleCount + 1
  }
  @action.bound eatApple (id) {
    const deleteApple = this.apples.find(apple => id === apple.id)
    this.eatedApples.push(deleteApple)
    this.apples = this.apples.filter(apple => id !== apple.id)
    this.appleCount = this.appleCount - 1
    this.eatedApplesCount = this.eatedApplesCount + 1
  }
  @computed get totalWeight () {
    let total = 0
    this.apples.forEach((item) => total = total + item.weight)
    return total
  }
  @computed get totalEatedWeight () {
    let total = 0
    this.eatedApples.forEach((item) => total = total + item.weight)
    return total
  }
}

const apples = new Apples()

export default apples